using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
using Progress = UnityEditor.Progress;
using Random = UnityEngine.Random;

namespace  Survivor
{
    public class ExpUpgradeSystem : AbstractSystem
    {
        public List<ExpUpgradeItem> items = new();
        private List<ExpUpgradeItem> weaponItems = new();
        public List<ExpUpgradeItem> itemsCopy;
        
        public int expUpgradeItemNum = 3;  // 一次性显示的升级项数量
        public int ExpUpgradeItemNum => Math.Min(items.Count, expUpgradeItemNum);
        protected override void OnInit()
        {
            items.Clear();

            #region 普通攻击 SimpleAbility
            
                var simpleAbilityItem = new ExpUpgradeItem()
                {
                    Key = "SimpleAbility",
                    Level = 0,
                    Weight = 20,
                    IconName = "SimpleSword"
                };
                #region  添加事件

                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv1\nAuto-Attack Nearby Enemies",
                    () =>
                    {
                        Global.SimpleAttackUnlock.Value = true;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv2\nDamage +3,Interval -0.25s ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 3;
                        Global.SimpleAttackInterval.Value -= 0.25f;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv3\nDamage +3,Count +3 ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 3;
                        Global.SimpleAttackCount.Value += 3;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv4\nDamage +3,Interval -0.25s ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 3;
                        Global.SimpleAttackInterval.Value -= 0.25f;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv5\nDamage +3,Interval -0.25s ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 3;
                        Global.SimpleAttackInterval.Value -= 0.25f;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv6\nDamage +5,Count+3",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 5;
                        Global.SimpleAttackCount.Value +=3;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv7\nDamage +5,Count+3 ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 5;
                        Global.SimpleAttackCount.Value +=4;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv8\nDamage +5,Range x1.5",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 5;
                        Global.SimpleAttackRange.Value *= 1.5f;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv9\nDamage +5, Interval -0.25s ",
                    () =>
                    {
                        Global.SimpleAttackDamage.Value += 5;
                        Global.SimpleAttackInterval.Value -= 0.25f;
                    });
                simpleAbilityItem.AddOnUpgrade("Simple Attack Lv10\nCount +10 !!! ",
                    () =>
                    {
                        Global.SimpleAttackCount.Value +=10;
                    });
                
                #endregion
                items.Add(simpleAbilityItem);
            
            #endregion

            #region  环绕攻击 SimpleCircle
            
                var simpleCircleItem = new ExpUpgradeItem()
                {
                    Key = "SimpleCircle",
                    Level = 0,
                    Weight = 20,
                    IconName = "RotateSword"
                };
                #region  添加事件
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv1\nA Sword Around You", () =>
                {
                    Global.SimpleCircleUnlock.Value = true;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv2\nDamage +3", () =>
                {
                    Global.SimpleCircleDamage.Value += 3;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv3\nDamage +3", () =>
                {
                    Global.SimpleCircleDamage.Value += 3;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv4\nDamage +3 Scale +40%", () =>
                {
                    Global.SimpleCircleDamage.Value += 3;
                    Global.SimpleCircleScale.Value *=  1.4f;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv5\nDamage +3 Scale +40%", () =>
                {
                    Global.SimpleCircleDamage.Value += 3;
                    Global.SimpleCircleScale.Value  *= 1.4f;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv6\nSpeed +40%", () =>
                {
                    Global.SimpleCircleSpinSpeed.Value = (int)(Global.SimpleCircleSpinSpeed.Value * 1.4f);
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv7\nDamage +5", () =>
                {
                    Global.SimpleCircleDamage.Value += 5;
                });
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv8\nDamage +5", () =>
                {
                    Global.SimpleCircleDamage.Value += 5;
                });                
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv9\nDamage +5", () =>
                {
                    Global.SimpleCircleDamage.Value += 5;
                });                
                simpleCircleItem.AddOnUpgrade("Simple SpinAttack Lv10\nSpeed X2 Scale X2 !!!", () =>
                {
                    Global.SimpleCircleSpinSpeed.Value = (int)(Global.SimpleCircleSpinSpeed.Value * 2f);
                    Global.SimpleCircleSpinSpeed.Value = (int)(Global.SimpleCircleSpinSpeed.Value * 2f);
                });
                
                
                
                
                #endregion
                items.Add(simpleCircleItem);
            
            #endregion

            #region 斧头攻击 SimpleAxe
            
                var simpleAxeItem = new ExpUpgradeItem()
                {
                    Key = "SimpleAxe",
                    Level = 0,
                    Weight = 20,
                    IconName = "Axe"
                };

                #region 添加事件

                simpleAxeItem.AddOnUpgrade("Simple Axe Lv1\nThrow a Large Axe", () =>
                {
                    Global.SimpleAxeUnlock.Value = true;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv2\nDamage +3", () =>
                {
                    Global.SimpleAxeDamage.Value += 3;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv3\nDamage +3, Interval -0.1s", () =>
                {
                    Global.SimpleAxeDamage.Value += 3;
                    Global.SimpleAxeInterval.Value -= 0.1f;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv4\nScale +25% Count +1", () =>
                {
                    Global.SimpleAxeScale.Value*=1.25f;
                    Global.SimpleAxeCount.Value += 1;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv5\nDamage +5 Count +1", () =>
                {
                    Global.SimpleAxeDamage.Value += 5;
                    Global.SimpleAxeCount.Value += 1;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv6\nScale +25% Interval -0.1s", () =>
                {
                    Global.SimpleAxeScale.Value*=1.25f;
                    Global.SimpleAxeInterval.Value -= 0.1f;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv7\nDamage +5", () =>
                {
                    Global.SimpleAxeDamage.Value += 5;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv8\nScale +25% Interval -0.1s", () =>
                {
                    Global.SimpleAxeScale.Value*=1.25f;
                    Global.SimpleAxeInterval.Value -= 0.1f;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv9\nScale +25% Damage +5", () =>
                {
                    Global.SimpleAxeScale.Value*=1.25f;
                    Global.SimpleAxeDamage.Value += 5;
                });
                simpleAxeItem.AddOnUpgrade("Simple Axe Lv10\nCount +6 !!!", () =>
                {
                    Global.SimpleAxeCount.Value += 6;
                });
                #endregion
                items.Add(simpleAxeItem);
            
            #endregion
            
            #region 小刀攻击 SimpleKnife
            
                var simpleKnifeItem = new ExpUpgradeItem()
                {
                    Key = "SimpleKnife",
                    Level = 0,
                    Weight = 20,
                    IconName ="SimpleKnife"
                };
                #region 添加事件

                simpleKnifeItem.AddOnUpgrade("Knife Lv1\nThrow Knife", () =>
                {
                    Global.SimpleKnifeUnlock.Value = true;
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv2\nDamage +1,Count +2", () =>
                {
                    Global.SimpleKnifeDamage.Value += 1;
                    Global.SimpleKnifeCount.Value += 2;
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv3\nDamage +1,Interval -0.15s", () =>
                {
                    Global.SimpleKnifeDamage.Value += 1;
                    Global.SimpleKnifeInterval.Value -= 0.15f;
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv4\nDamage +1,Count +2", () => { 
                    Global.SimpleKnifeDamage.Value += 1;
                    Global.SimpleKnifeCount.Value += 2; });
                simpleKnifeItem.AddOnUpgrade("Knife Lv5\nCount +1", () => { 
                    Global.SimpleKnifeCount.Value += 1; });
                simpleKnifeItem.AddOnUpgrade("Knife Lv6\nDamage +1", () => { 
                    Global.SimpleKnifeDamage.Value += 1; 
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv7\nInterval -0.15s", () => { 
                    Global.SimpleKnifeInterval.Value -= 0.15f; 
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv8\nDamage +2", () => { 
                    Global.SimpleKnifeDamage.Value += 2; });
                simpleKnifeItem.AddOnUpgrade("Knife Lv9\nDamage +1,Count +1", () => { 
                    Global.SimpleKnifeDamage.Value += 1;
                    Global.SimpleKnifeCount.Value += 1;
                });
                simpleKnifeItem.AddOnUpgrade("Knife Lv10\nCount +3", () => { 
                    Global.SimpleKnifeCount.Value += 3; });
                
                
                
                // var description = new string[] {
                //     "Unlock Knife",
                //     "Enhance Knife Damage",
                //     "Shorten Knife Interval",
                //     "Increasing Knife Number"};
                // dict.Add(description[0],(item) => { Player.Default.SimpleKnife.Show();});
                // dict.Add(description[1],(item) => { Global.SimpleKnifeDamage.Value += 1; });
                // dict.Add(description[2],(item) => { Global.SimpleKnifeInterval.Value *= 0.8f; });
                // dict.Add(description[3],(item) => { Global.SimpleKnifeNumber.Value +=1;});
                // FunAddOnUpgrade(simpleKnifeItem, description[0]);
                // for (int i = 0; i < 2; i++)
                // {
                //     FunAddOnUpgrade(simpleKnifeItem, description[1]);
                //     FunAddOnUpgrade(simpleKnifeItem, description[2]);
                //     FunAddOnUpgrade(simpleKnifeItem, description[3]);
                // }
                items.Add(simpleKnifeItem);

                #endregion
            
            #endregion
            
            #region 暴击率
            var criticalRateItem = new ExpUpgradeItem()
            {
                Key = "CriticalRate",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName = "Critical"
            };
            for (int i = 1; i <= 5; i++)
            {
                criticalRateItem.AddOnUpgrade($"CriticalRate Lv{i}\nCriticalRate +15%",
                    () => { Global.CriticalRate.Value += 0.15f; });
            }
            items.Add(criticalRateItem);
            #endregion
            
            #region 伤害倍率
            var damageRateItem = new ExpUpgradeItem()
            {
                Key = "DamageRate",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName ="Damage"
            };
            for (int i = 1; i <= 5; i++)
            {
                damageRateItem.AddOnUpgrade($"DamageRate Lv{i}\nDamage +25%",
                    () => { Global.DamageRate.Value += 0.25f; });
            }
            items.Add(damageRateItem);
            #endregion

            #region 飞射物数量
            var flyThingCountItem = new ExpUpgradeItem()
            {
                Key = "FlyThingCount",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName ="Fly"
            };
            for (int i = 1; i <= 2; i++)
            {
                flyThingCountItem.AddOnUpgrade($"FlyThingCount Lv{i}\nCount +1",
                    () => { Global.FlyThingCount.Value +=1; });
            }
            
            items.Add(flyThingCountItem);
            #endregion

            #region 移动速度
            var moveSpeedRateItem = new ExpUpgradeItem()
            {
                Key = "MoveSpeed",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName = "Movement"
            };
            for (int i = 1; i <= 5; i++)
            {
                moveSpeedRateItem.AddOnUpgrade($"MoveSpeed Lv{i}\nMoveSpeed +10%",
                    () => { Global.MoveSpeedRate.Value +=0.1f; });
            }
            
            items.Add(moveSpeedRateItem);
            #endregion

            #region  拾取范围
            var pickUpRangeRateItem = new ExpUpgradeItem()
            {
                Key = "PickUpRange",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName = "Collectable"
            };
            for (int i = 1; i <= 5; i++)
            {
                pickUpRangeRateItem.AddOnUpgrade($"PickUpRange Lv{i}\nPickUpRange +100%",
                    () => { Global.PickUpRangeRate.Value += 1f; });
            }
            
            items.Add(pickUpRangeRateItem);
            #endregion

            #region 经验倍率
            var expRateItem = new ExpUpgradeItem()
            {
                Key = "Exp",
                Level = 0,
                Weight = 20,
                IsWeapon = false,
                IconName = "Exp"
            };
            for (int i = 1; i <= 5; i++)
            {
                expRateItem.AddOnUpgrade($"Exp Lv{i}\nExp +50%",
                    () => { Global.ExpRate.Value += 0.5f; });
            }
            
            items.Add(expRateItem);
            #endregion


            // 用于随机
            itemsCopy = new(items);
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].IsWeapon)
                {
                    weaponItems.Add(items[i]);
                }
            }
        }


        /// <summary>
        /// 获得随机升级项，排除被动道具
        /// </summary>
        /// <returns></returns>
        public ExpUpgradeItem GetRandomItemsExcludePassive()
        {
            int index = Random.Range(0, weaponItems.Count);
            return weaponItems[index];
        }

        public List<ExpUpgradeItem> GetRandomItems(int n = -1)
        {
            for (int i = itemsCopy.Count-1; i>=0; i--)
            {
                if ( itemsCopy[i].IsUpgradeMaxLevel)
                {
                    itemsCopy.RemoveAt(i);
                }
            }
            
            List<ExpUpgradeItem> result = new List<ExpUpgradeItem>();
            
            // 参数校验
            if (n == -1) n = ExpUpgradeItemNum;
            
            // 创建临时可修改副本
            List<ExpUpgradeItem> tempItems = new List<ExpUpgradeItem>(itemsCopy);
            
            for (int i = 0; i < n; i++)
            {
                
                // 动态计算当前权重总和
                int weightSum = 0;
                List<int> cumulativeWeights = new List<int>();
                foreach (var item in tempItems)
                {
                    weightSum += item.Weight;
                    cumulativeWeights.Add(weightSum);
                }
                
                // 处理全零权重情况
                if (weightSum <= 0) break;
                
                // 执行加权随机选择
                int randomValue = Random.Range(0, weightSum);
                int selectedIndex = 0;
                for (; selectedIndex < cumulativeWeights.Count; selectedIndex++)
                {
                    if (randomValue < cumulativeWeights[selectedIndex]) break;
                }
                
                // 记录结果并移除已选项
                result.Add(tempItems[selectedIndex]);
                tempItems.RemoveAt(selectedIndex);
            }
            
            return result;
        }

        public void Reset()
        {
            foreach (var item in itemsCopy)
            {
                item.Level = 0;
            }

            itemsCopy.Clear();
            OnInit();
        }
    }

}